package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.domain.SystemAccount;
import cn.wolfcode.p2p.business.domain.SystemAccountFlow;
import cn.wolfcode.p2p.business.mapper.SystemAccountFlowMapper;
import cn.wolfcode.p2p.business.service.ISystemAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service@Transactional
public class SystemAccountFlowServiceImpl implements ISystemAccountFlowService {
    @Autowired
    private SystemAccountFlowMapper systemAccountFlowMapper;

    @Override
    public int save(SystemAccountFlow systemAccountFlow) {
        return systemAccountFlowMapper.insert(systemAccountFlow);
    }

    private void createFlow(SystemAccount systemAccount, BigDecimal amount, int actionType, String note){
        SystemAccountFlow accountFlow = new SystemAccountFlow();
        accountFlow.setActionTime(new Date());
        accountFlow.setActionType(actionType);
        accountFlow.setAmount(amount);
        accountFlow.setFreezeAmount(systemAccount.getFreezedAmount()); //冻结金额
        accountFlow.setUsableAmount(systemAccount.getUsableAmount()); //可用金额
        accountFlow.setNote(note);
        this.save(accountFlow);
    }
    @Override
    public void createReceiveAccountManagementChargeFlow(SystemAccount systemAccount, BigDecimal amount) {
        createFlow(systemAccount,amount,Constants.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE, "系统收到借款管理费"+amount+"元");
    }

    @Override
    public void createReceiveInterestManagerChargeFlow(SystemAccount systemAccount, BigDecimal amount) {
        createFlow(systemAccount,amount,Constants.SYSTEM_ACCOUNT_ACTIONTYPE_INTREST_MANAGE_CHARGE, "系统收到利息管理费"+amount+"元");
    }
}
